<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%@ taglib uri="/sinopac-fn" prefix="fn" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<title>集團戶管理</title>
		<link rel="stylesheet" href="${fn:url('/form/CSS/tabs.css')}">
		<script type="text/javascript" src="${fn:url('/js/jQuery/jquery-1.8.2.min.js')}"></script>
		<script type="text/javascript" src="${fn:url('/js/jQuery/jquery_DOMWindow.js')}"></script>
		<script type="text/javascript" src="${fn:url('/js/tools/formUtil.js')}"></script>
		<script>
			$(document).ready(function(){
				if($('#errorMessage_a').val() !=""){
					alert($('#errorMessage_a').val());		
				}
				
				$('#tokenESN').click(function(){
					var self = $(this);
					var voValue = self.attr('voValue');
					var form = $('#testForm1');
					form.prop('action', '${fn:url('/Index/chkCustId.action')}');
					form.append('<input type="hidden" name="function_num" value="4" />');
					form.append('<input type="hidden" name="token_esn" value="'+voValue+'" />');
					form.submit();
				});
				
				$('a[name=relateCustId]').click(function(){
					var self = $(this);
					var voValue = self.attr('voValue');
					var form = $('#testForm1');
					form.prop('action', '${fn:url('/Index/chkCustId.action')}');
					form.append('<input type="hidden" name="function_num" value="2" />');
					form.append('<input type="hidden" name="custId" value="'+voValue+'" />');
					form.submit();
				});
			});

			//新增btnClick 
			function getNewRelateCustId(){
				var str = "";
				//str = $('#newCustId01_a').val()+'-'+$('#newCustId02_a').val()+'-'+ $('#newCustId03_a').val();
				str = $.joinId('#newCustId01_a', '#newCustId02_a', '#newCustId03_a');
				$('#nrc').val(str);
				
				var f = $('#popupNewId');
				f.prop('action','/sinopac/GroupManagement/addNew.action');

				f.submit();
				
			}
			
			//關聯客戶表格  移除btnClick 
			function newRelateCustID(){ 
			    $.openDOMWindow({ 
			    	windowSourceID:'.newIdDiv',
			    	height:110,  
			        width:370, 
			        overlay:1, 
			        overlayColor: '#A9A9A9',
			        windowPadding:20,
			        modal:1,
			        borderColor:'steelblue',
			        borderSize:'2', 
			        positionLeft:120, 
			        positionTop:-150,
			    }); 
			    return false; 
			}; 
			
			//關聯客戶表格  移除btnClick  
			var j_index_d = 0;
			function deleteRow(j_index_d, relateCustId){
				
				//$('moveOut_a').submit;
				//console.info([j_index,relateCustId]);
				///sinopac/GroupManagement/remove.action
				var f = $('#testForm1');
				f.prop('action','/sinopac/GroupManagement/remove.action');
				//console.info('<input type="hidden" name="relateCustId" value="'+relateCustId+'" />');
				//f.append('<input type="hidden" name="relateCustId" value="'+relateCustId+'" />');
				$('input[name=relateCustId]').val(relateCustId);
				f.submit();
			}
			
			//關聯客戶表格  設定btnClick 
			var j_index_u = 0;
			function updateData(j_index_u, relateCustId){

				var u = $('#testForm1');
				u.prop('action','/sinopac/GroupManagement/update.action');
				$('input[name=relateCustId]').val(relateCustId);
				u.submit();
			}

			function chkApply(){
				if($('#is_group_main').val() == "0" && $('#is_group').val() == "1"){
					alert("此帳戶並非主帳戶，無法進行申請配發TOKEN ESN");
				}else{
					var form = $('#testForm1');
					form.prop('action', '${fn:url('/CustomerApply/allotmentToken.action')}');
					form.submit();	
				}
			}
		</script>
	</head>
	<body>
		<%@ include file="/form/include/title.jsp" %>
		<div id="newCustomerID" class="newIdDiv" style="display:none">
			<s:form id="popupNewId">
				<table align="center">
					<tr align="left">
						<td align="left"><s:label value="請輸入客戶ID新增至關聯" theme="simple"/></td>
						<td align="left"><s:textfield name="newCustId01" id="newCustId01_a" theme="simple" maxlength="18" size="18"/></td>
						<td>-</td>
						<td align="left"><s:textfield name="newCustId02" id="newCustId02_a" theme="simple" maxlength="1" size="1"/></td>
						<td>-</td>
						<td align="left"><s:textfield name="newCustId03" id="newCustId03_a" theme="simple" maxlength="2" size="2"/></td>
						<td><s:hidden name="newRelateCustId" id="nrc"></s:hidden></td>
					</tr>
					<tr height="50">
					</tr>
					<tr>
						<td><button onclick="getNewRelateCustId()">確定</button></td>
						<td><s:submit value="取消" id="btnNo" cssClass="closeDOMWindow" theme="simple"/></td>			
					</tr>	
				</table>
			</s:form>
		</div>
		<div align="left">
			<s:form id="testForm1">
				<s:hidden name="mainAccVO[0].is_group_main" id="is_group_main"/>
				<s:hidden name="mainAccVO[0].is_group" id="is_group"/>
				<input type="hidden" name="relateCustId" />
				<s:hidden id="errorMessage_a" name="errorMessage"/>
				<table class="table-style" id="mainAccList_a" align="center">
					<tr>
						<th>客戶ID</th>
						<th>客戶名稱</th>
						<th>AO Code</th>
						<th>TOKEN ESN</th>
						<th>OTP狀態</th>
						<th>主帳戶</th>
					</tr>
					<s:iterator value="mainAccVO" status="i" var="a">
						<tr>
							<td align="center"><s:property value="custId"/></td>
							<td><s:property value="custName"/></td>
							<td align="center"><s:property value="aoCode"/></td>
							<s:if test="tokenESN == null">								
								<s:if test="token_status">								
									<td align="center">配發中</td>									
								</s:if>
								<s:else>
									<td align="center"><a onclick="chkApply()" href="#">未配發</a></td>
								</s:else>
							</s:if>
							<s:else>
								<td><a id="tokenESN" href="#" voValue="${tokenESN}"><s:property value="tokenESN"/></a></td>
							</s:else>
							<!-- OTP狀態轉換　start -->
							<s:if test="otpStatus == '00'.toString()">
								<td>未啟用</td>
							</s:if>
							<s:elseif test="otpStatus == '01'.toString()">
								<td>正常</td>
							</s:elseif>
							<s:elseif  test="otpStatus == '02'.toString()">
								<td>已暫禁</td>
							</s:elseif>
							<s:elseif test="otpStatus == '03'.toString()">
								<td>已註銷</td>
							</s:elseif>
							<td align="center" style="display: none;"><s:property value="otpStatus"/></td>
							<!-- OTP狀態轉換　end -->
							
						<!--	<s:if test="tokenESN == ''"></s:if> -->
								<!-- 是否為主帳戶轉換　start -->
								<s:if test="mainAccVO[0].is_group == 1">
									<s:if test="mainAcc == '0'.toString()">
										<s:if test="mainAccVO[0].token_status == false">
											<td><a name="mainAcct" onclick="updateData(${i.index},'${a.custId}')" id="mainAcct_a" href="#">設定</a></td>
										</s:if>
										<s:else>
											<td>&nbsp;</td>
										</s:else>
									</s:if>
									<s:else>
										<td>是</td>
									</s:else>
								</s:if>
								<s:else>
									<td>&nbsp;</td>
								</s:else>
								<td align="center" style="display: none;"><s:property value="mainAcc"/></td>
								<!-- 是否為主帳戶轉換　end -->								
						</tr>
					</s:iterator>
				</table>
				<table align="center">
					<tr height="40"/>			
					<tr align="center">
						<td><s:label theme="simple">關聯客戶ID:</s:label></td>
						<!--修改if判斷式　如果客戶已有token_esn(不管為個人戶TOKEN或是集團戶TOKEN)　則不可增加關聯帳戶 -->
						
							<s:if test='%{userRoles != "2"}'>
								
									<td><a theme="simple" onclick="newRelateCustID()" href="#">新增</a></td>
								
							</s:if>
						
						<!--修改if判斷式　如果客戶已有token_esn(不管為個人戶TOKEN或是集團戶TOKEN)　則不可增加關聯帳戶 -->			
					</tr>
				</table>
				<table class="table-style" id="relateAccList_a" align="center">
					<tr height="20">
					<tr>
						<th>客戶ID</th>
						<th>客戶名稱</th>
						<th>AO Code</th>
						<th>OTP狀態</th>
						<th>主帳戶</th>
						<th>移除</th>
					</tr>
					<s:iterator value="relateAccVO" status="j" var="b">
						<tr>
							<td style="display: none;"><s:property value="#j.index"/></td>
							<s:if test='%{userRoles != "2"}'>
								<td align="center"><a name="relateCustId" voValue="${relateCustId}" id="relateCustId_a"  href="#"><s:property value="relateCustId"/></a></td>
							</s:if>
							<s:else>
								<td align="center"><s:property value="relateCustId"/></td>
							</s:else>
							<td><s:property value="relateCustName"/></td>
							<td align="center"><s:property value="relateAoCode"/></td>
							<!-- OTP狀態轉換　start -->
							<s:if test="relateOtpStatus == '00'.toString()">
								<td align="center">未啟用</td>
							</s:if>
							<s:elseif test="relateOtpStatus == '01'.toString()">
								<td align="center">正常</td>
							</s:elseif>
							<s:elseif  test="relateOtpStatus == '02'.toString()">
								<td align="center">已暫禁</td>
							</s:elseif>
							<s:elseif test="relateOtpStatus == '03'.toString()">
								<td align="center">已註銷</td>
							</s:elseif>
							<td style="display: none;"><s:property value="relateOtpStatus"/></td>
							<!-- OTP狀態轉換　end -->
									
							<!-- 是否為主帳戶轉換　start -->
							<s:if test="setMainAcct == '0'.toString()">
								<s:if test='%{userRoles == "2"}'>
									<td align="center">否</td>
								</s:if>
								<s:else>
									<s:if test="mainAccVO[0].token_status == false">
										<td align="center"><a  name="setMainAcct" onclick="updateData(${j.index},'${b.relateCustId}')" id="setMainAcct_a" href="#">設定</a></td>
									</s:if>	
									<s:else>
										<td>&nbsp;</td>
									</s:else>								
								</s:else>							
							</s:if>
							<s:else>
								<td align="center">是</td>
							</s:else>
							<td style="display: none;"><s:property value="setMainAcct"/></td>
							<!-- 是否為主帳戶轉換　end -->
							<!-- 是否可以移除　start -->
							
							<!-- 增加if判斷式　如果客戶已有token_esn(不管為個人戶TOKEN或是集團戶TOKEN)　則不可移除關聯帳戶 -->
							
								<s:if test="setMainAcct == '0'.toString()">
									<s:if test="relateOtpStatus == '00'">
										<s:if test='%{userRoles != "2"}'>
											<td align="center"><a name="moveOut" onclick="deleteRow(${j.index},'${b.relateCustId}')" id="moveOut_a" href="#">移除</a></td>
										</s:if>	
										<s:else>
											<td></td>
										</s:else>									
									</s:if>
									<s:else>
										<td></td>
									</s:else>
								</s:if>
								<s:else>
									<td>&nbsp;</td>
								</s:else>
							
							<!-- 增加if判斷式　如果客戶已有token_esn(不管為個人戶TOKEN或是集團戶TOKEN)　則不可移除關聯帳戶 -->
							<td style="display: none;"><s:property value="%{ moveOut }"/></td>	
							<!-- 是否可以移除　start -->
						</tr>
					</s:iterator>
				</table>
			</s:form>
		</div>
	</body>
</html>